package com.ws.base.db.common;

import java.util.Map;

import com.ws.base.common.util.WSUtil;

/**
 * DBUtil
 * 
 * @author ewangshi
 */
public final class DBUtil {
    private final static String FLAG_START = "<\\[";

    private final static String FLAG_END = "]>";

    public final String sqlStrStationsHandle(String sql, Map<String, Object> map) {
        sql = this.injectionString(sql, map);
        StringBuffer buffer = new StringBuffer();

        String[] array = sql.split(FLAG_START);

        if (!WSUtil.isEmpty(array)) {
            for (int i = 0; i < array.length; i++) {
                if (array[i].indexOf(FLAG_END) != -1) {
                    buffer.append(checkerStations(array[i], map, FLAG_END));
                } else {
                    buffer.append(array[i]);
                }
            }
        }
        return buffer.toString();
    }

    private final String checkerStations(String str, Map<String, Object> map,
            String flagStr) {
        String[] arr = {};

        if (FLAG_END.equals(flagStr)) {
            arr = str.split(FLAG_END);
            str = arr[0];
        }
        String temp = "";

        for (int i = 0; i < str.length(); i++) {
            if (':' == str.charAt(i)) {
                for (int j = i + 1; j < str.length(); j++) {
                    if (' ' == str.charAt(j) || ')' == str.charAt(j)
                            || ']' == str.charAt(j) || '\'' == str.charAt(j)
                            || '%' == str.charAt(j) || ',' == str.charAt(j)) {
                        temp = str.substring(i + 1, j);
                        if ((null == map || null == map.get(temp))
                                && FLAG_END.equals(flagStr)) {
                            return " " + (arr.length > 1 ? arr[1] : "");
                        } else if ("%>".equals(flagStr) && !WSUtil.isEmpty(map)
                                && null != map.get(temp)) {
                            return str.substring(0, i)
                                    + map.get(temp).toString()
                                    + str.substring(str.indexOf("%>") + 2)
                                    + (arr.length > 1 ? arr[1] : "");
                        } else if ("%>".equals(flagStr)
                                && (WSUtil.isEmpty(map) || null == map
                                        .get(temp))) {
                            return str.substring(str.indexOf("%>") + 2)
                                    + (arr.length > 1 ? arr[1] : "");
                        }
                        i = j;
                        break;
                    } else if (j == (str.length() - 1)) {
                        temp = str.substring(i + 1, j + 1);
                        if ((null == map || null == map.get(temp))
                                && FLAG_END.equals(flagStr)) {
                            return " " + (arr.length > 1 ? arr[1] : "");
                        } else if ("%>".equals(flagStr) && !WSUtil.isEmpty(map)
                                && null != map.get(temp)) {
                            return str.substring(0, i)
                                    + map.get(temp).toString()
                                    + str.substring(str.indexOf("%>") + 2)
                                    + (arr.length > 1 ? arr[1] : "");
                        } else if ("%>".equals(flagStr)
                                && (WSUtil.isEmpty(map) || null == map
                                        .get(temp))) {
                            return str.substring(str.indexOf("%>") + 2)
                                    + (arr.length > 1 ? arr[1] : "");
                        }
                        i = j;
                        break;
                    }
                }
            }
        }

        return str + (arr.length > 1 ? arr[1] : "");
    }

    private final String injectionString(String sql, Map<String, Object> map) {
        StringBuffer buffer = new StringBuffer();
        String[] array = sql.split("<%");

        if (!WSUtil.isEmpty(array)) {
            for (int i = 0; i < array.length; i++) {
                if (array[i].indexOf("%>") != -1) {
                    buffer.append(checkerStations(array[i], map, "%>"));
                } else {
                    buffer.append(array[i]);
                }
            }
        }

        return buffer.toString();
    }

}
